class Solution {
public:
    int numOfMinutes(int n, int headID, vector<int>& manager, vector<int>& informTime) {
        vector<vector<int>>subordinates(n);
        for(int i=0;i<n;++i){
            if(manager[i] != -1){
                subordinates[manager[i]].push_back(i);
            }
        }

        return dfs(headID,subordinates,informTime);
    }
private:
    int dfs(int node,const vector<vector<int>>& subordinates,const vector<int>& informTime){
        int maxTime = 0;
        for(int sub:subordinates[node]){
            maxTime = max(maxTime,dfs(sub,subordinates,informTime));
        }
        return informTime[node]+maxTime;
    }
};